﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace HW7_g2
{
    public partial class Form1 : Form
    {
        static string ID = "";
        static string pattern = @"^\d{17}(?:\d|X)$";
        public Form1()
        {
            InitializeComponent();
        }



        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            ID = textBox1.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";
            string birth = ID.Substring(6, 8).Insert(6, "-").Insert(4, "-");
            DateTime time = new DateTime();

            int[] arr_weight = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };     // 加权数组
            string[] id_last = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };   // 校验数组
            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += arr_weight[i] * int.Parse(ID[i].ToString());
            }
            int result = sum % 11;
            if (Regex.IsMatch(ID, pattern))
            {
                if (DateTime.TryParse(birth, out time))
                {
                    if (id_last[result] == ID[17].ToString())
                    {
                        textBox2.Text = "合法";
                    }
                    else
                    {
                        textBox2.Text = "检验位错误";
                    }
                }
                else
                {
                    textBox2.Text = "出生日期非法";
                }
            }
            else
            {
                textBox2.Text = "格式非法";
            }

        }
    
    }
}
